免费配置站点https

简介

谷歌提倡网站使用https,而且如果你的网站是http的话,浏览器会显示不安全。而微信小程序更是直接只能通过https的站点,才能调用后台服务。网站启用https访问,首先需要一个证书机构颁发的ssl证书,目前给个人免费颁发证书的机构,比较好的是:Let’s Encrypt。而Let’s Encrypt的证书现在支持自动颁发安装,客户端工具为certbot。通过certbot工具,我们可以很便捷的获取证书,再加上定时任务就能很便捷的自动续约了。

安装

1
2
3
4
#官网下载工具
cd /root
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

检查机器的80,443端口是否被占用,一般80端口会被nginx占用了,需要先停掉nginx。如果是阿里云的话还需要配置端口的安全组规则。

1
2
#停止nginx
service nginx stop

生成证书(如果后期生成报错,先删除原有certbot-auto,按第一步重新下载certbot即可)

1
2
#使用-d追加多个域名,使用时请将邮箱换成自己的邮箱
./certbot-auto certonly --standalone --email xxxx@xx.com --agree-tos -d xxx.com -d api.xxx.com -d www.xxx.me

查看生成的证书

1
ls /etc/letsencrypt/live/

nginx配置证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 443 ssl;
server_name api.xxx.com;
#证书位置
ssl_certificate /etc/letsencrypt/live/api.xxx.com/fullchain.pem;
# 私钥位置
ssl_certificate_key /etc/letsencrypt/live/api.xxx.com/privkey.pem;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
//服务地址
proxy_pass http://127.0.0.1:30519/;
}
}

启动nginx

1
service nginx start

编写更新脚本renew-cert.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

# 停止nginx
/sbin/service nginx stop

# 续签
# --force-renew 强制更新
/root/certbot-auto renew --force-renew

# 启动nginx
/sbin/service nginx start

chmod a+x renew-cert.sh

###配置定时任务

1
2
//crontab定时任务自动更新证书
0 3 1 */2 * /root/renew-cert.sh >/root/crontab.log 2>&1